﻿
namespace SvCore.Common;

public static class NameMgr
{
    /// <summary>
    /// 获取名字
    /// </summary>
    /// <param name="gender">性别 0=男性 1=女性</param>
    /// <returns></returns>
    public static string GetName(int gender)
    {
        string name = "无名氏";
        string surname = "";
        string fame = "";
        if (ConfSurnameExt.Count > 0)
        {
            surname = ConfSurnameExt[Util.Rand(ConfSurnameExt.Count)];
        }
        if (gender == 0)
        {
            if (ConfMalenameExt.Count > 0)
            {
                fame = ConfMalenameExt[Util.Rand(ConfMalenameExt.Count)];
            }
        }
        else
        {
            if (ConfFemalenameExt.Count > 0)
            {
                fame = ConfFemalenameExt[Util.Rand(ConfFemalenameExt.Count)];
            }
        }
        if (surname.Length + fame.Length > 6)
        {
            name = fame;
        }
        else
        {
            name = surname + fame;
        }
        if (name == "")
        {
            name = "无名氏";
        }
        return name;
    }

    /// <summary>
    /// 获取名字
    /// </summary>
    /// <returns></returns>
    public static string GetName()
    {
        int gender = Util.Rand(2);
        return GetName(gender);
    }

    /// <summary>
    /// 判断账号是否合法（只能使用英文和数字）
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static bool CheckAccountLegal(string v)
    {
        string pattern = @"^[A-Za-z0-9]+$";
        var rx = new Regex(pattern);
        return rx.IsMatch(v);
    }

    /// <summary>
    /// 判断密码是否合法（只能使用英文和数字）
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static bool CheckPasswdLegal(string v)
    {
        string pattern = @"^[A-Za-z0-9]+$";
        var rx = new Regex(pattern);
        return rx.IsMatch(v);
    }

    /// <summary>
    /// 判断中文名是否合法
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static bool CheckNameLegal(string v)
    {
        return CheckNameLegalLanguage(v, SvInfo.Language);
    }

    /// <summary>
    /// 判断中文名是否合法
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static bool CheckNameLegalLanguage(string v, ELanguage Language)
    {
        //中文简体 @"^[\u4e00-\u9fa5]+$"
        //韩语 @"^[\uac00-\ud7ff]+$"
        string pattern;
        if (Language == ELanguage.英语)
        {
            pattern = @"^[A-Za-z0-9]+$";
            var rx = new Regex(pattern);
            return rx.IsMatch(v);
        }
        else if (Language == ELanguage.中文简体
            || Language == ELanguage.中文繁体)
        {
            pattern = @"^[A-Za-z0-9\u4e00-\u9fa5]+$";
            var rx = new Regex(pattern);
            return rx.IsMatch(v);
        }
        //else if (Language == ELanguage.韩语)
        //{
        //    pattern = @"^[A-Za-z0-9\uac00-\ud7ff]+$";
        //    var rx = new Regex(pattern);
        //    return rx.IsMatch(v);
        //}
        return true;
    }

    /// <summary>
    /// 判断是否屏蔽字
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static bool CheckBadWords(string v)
    {
        if (ConfBadwordsExt.Contains(v))
        {
            return true;
        }
        return false;
    }

    //检查姓名
    public static EProtocolResult CheckName(string name)
    {
        //名字不能为空
        if (name.Length <= 0 || name == "" || name.IsNullOrEmpty())
        {
            return EProtocolResult.名字长度不符;
        }
        var ByteLength = name.ByteLength();
        if (ByteLength < EGlobalId.昵称长度最小.ValueInt()
            || ByteLength > EGlobalId.昵称长度最大.ValueInt())
        {
            return EProtocolResult.名字长度不符;
        }
        //不能含有非法字符
        if (!CheckNameLegal(name))
        {
            return EProtocolResult.名字含有不合法或者不雅字符;
        }
        //不能含有非法字符
        if (CheckBadWords(name))
        {
            return EProtocolResult.名字含有不合法或者不雅字符;
        }
        return EProtocolResult.成功;
    }
}
